<?php
/**
* @file $Id: Other_Info.inc.php 492 2007-05-08 14:48:32Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['modfunc']=='delete' && User('PROFILE')=='admin')
{
	if(!$_REQUEST['delete_ok'] && !$_REQUEST['delete_cancel'])
		echo '</FORM>';
	if(DeletePrompt(_('log entry')))
	{
		DBQuery("DELETE FROM STUDENT_LOG_ENTRIES WHERE ID='$_REQUEST[id]'");
		unset($_REQUEST['modfunc']);
	}
}

if($_REQUEST['modfunc']!='delete' && $_REQUEST['category_id']=='required')
{
	include_once($staticpath.'modules/Students/includes/functions.php');
	if($_REQUEST['category_id']=='required')
		$fields_RET = DBGet(DBQuery("SELECT * FROM CUSTOM_FIELDS WHERE CATEGORY_ID!='1' AND (REQUIRED='Y' OR (DEFAULT_SELECTION IS NOT NULL)) ORDER BY CATEGORY_ID,TITLE"),array(),array('TYPE'));
	else
		$fields_RET = DBGet(DBQuery("SELECT * FROM CUSTOM_FIELDS WHERE CATEGORY_ID='$_REQUEST[category_id]' ORDER BY TITLE"),array(),array('TYPE'));
	$radio_count = count($fields_RET['radio']);
	
	if(UserStudentID())
	{
		$custom_RET = DBGet(DBQuery("SELECT * FROM STUDENTS WHERE STUDENT_ID='".UserStudentID()."'"));
		$value = $custom_RET[1];
	}
	
	echo '<TABLE cellpadding=5>';
	$i = 1;
	if(count($fields_RET['text']))
	{
		foreach($fields_RET['text'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			if($field['MAX_LENGTH'])
			{
				$options = ' maxlength='.$field['MAX_LENGTH'];
				if($field['MAX_LENGTH']<30)
					$options .= ' size='.$field['MAX_LENGTH'];
			}
			else
				$options = false;

			if(($i-1)%3==0)
				echo '<TR>';
			echo '<TD>';			
			echo _makeTextInput('CUSTOM_'.$field['ID'],$field['TITLE'],$options);
			echo '</TD>';
			if($i%3==0)
				echo '</TR>';
			else
				echo '<TD width=50></TD>';
			$i++;	
		}
	}
	
	if(count($fields_RET['numeric']))
	{
		foreach($fields_RET['numeric'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			if(($i-1)%3==0)
				echo '<TR>';
			echo '<TD>';
			echo _makeTextInput('CUSTOM_'.$field['ID'],$field['TITLE'],'','size=5 maxlength=10');
			echo '</TD>';
			if($i%3==0)
				echo '</TR>';
			else
				echo '<TD width=50></TD>';
			$i++;	
		}
	}
	
	if(count($fields_RET['date']))
	{
		foreach($fields_RET['date'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			if(($i-1)%3==0)
				echo '<TR>';
			echo '<TD>';			
			echo _makeDateInput('CUSTOM_'.$field['ID'],$field['TITLE'],'');
			echo '</TD>';
			if($i%3==0)
				echo '</TR>';
			else
				echo '<TD width=50></TD>';
			$i++;	
		}
	}
	if(count($fields_RET['select']))
	{
		foreach($fields_RET['select'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			if(($i-1)%3==0)
				echo '<TR>';
			echo '<TD>';			
			echo _makeSelectInput('CUSTOM_'.$field['ID'],$field['TITLE']);
			echo '</TD>';
			if($i%3==0)
				echo '</TR>';
			else
				echo '<TD width=50></TD>';
			$i++;
		}
	}
	echo '</TABLE><BR>';
	
	$i = 1;
	if(count($fields_RET['textarea']) || count($fields_RET['multiple']) || count($fields_RET['log']))
		echo '<TABLE cellpadding=5>';
	if(count($fields_RET['textarea']))
	{
		foreach($fields_RET['textarea'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			if($i%2!=0)
				echo '<TR>';
			echo '<TD>';			
			echo _makeTextareaInput('CUSTOM_'.$field['ID'],$field['TITLE']);
			echo '</TD>';
			if($i%2==0)
				echo '</TR>';
			$i++;
		}
	}
	
	if(count($fields_RET['multiple']))
	{
		foreach($fields_RET['multiple'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			if($i%2!=0)
				echo '<TR>';
			echo '<TD>';			
			echo _makeMultipleInput('CUSTOM_'.$field['ID'],$field['TITLE']);
			echo '</TD>';
			if($i%2==0)
				echo '</TR>';
			$i++;
		}
	}
	
	if(count($fields_RET['log']))
	{
		foreach($fields_RET['log'] as $field)
		{
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			$link = array();
			$columns = array();
			$count_fields = 0;
			echo '<TR><TD colspan=2>';
			$log_options = $field['SELECT_OPTIONS'];
			$table = $field['ID'];
			$functions = array('LOG_CHECKBOX'=>'_makeLogCheckbox','LOG_SELECT'=>'_makeLogSelect','LOG_DATE'=>'_makeLogDate','LOG_TEXT1'=>'_makeLogText','LOG_TEXT2'=>'_makeLogText','LOG_TEXT3'=>'_makeLogText','LOG_TEXT4'=>'_makeLogText','LOG_NUMERIC'=>'_makeLogText','LOG_TEXTAREA'=>'_makeLogTextArea');
			if($field['LOG_CHECKBOX'])
			{
				$count_fields++;
				$log_functions['LOG_CHECKBOX'] = $functions['LOG_CHECKBOX'];
				$columns['LOG_CHECKBOX'] = $field['LOG_CHECKBOX_TITLE'];
				$link['add']['html']['LOG_CHECKBOX'] = $functions['LOG_CHECKBOX']('','LOG_CHECKBOX');
			}
			if($field['LOG_SELECT'])
			{
				$count_fields++;
				$log_functions['LOG_SELECT'] = $functions['LOG_SELECT'];
				$columns['LOG_SELECT'] = $field['LOG_SELECT_TITLE'];
				$link['add']['html']['LOG_SELECT'] = $functions['LOG_SELECT']('','LOG_SELECT');
			}
			if($field['LOG_DATE'])
			{
				$count_fields += 2;
				$log_functions['LOG_DATE'] = $functions['LOG_DATE'];
				$columns['LOG_DATE'] = $field['LOG_DATE_TITLE'];
				$link['add']['html']['LOG_DATE'] = $functions['LOG_DATE']('','LOG_DATE');
			}
			if($field['LOG_NUMERIC'])
			{
				$count_fields++;
				$log_functions['LOG_NUMERIC'] = $functions['LOG_NUMERIC'];
				$columns['LOG_NUMERIC'] = $field['LOG_NUMERIC_TITLE'];
				$link['add']['html']['LOG_NUMERIC'] = $functions['LOG_NUMERIC']('','LOG_NUMERIC');
			}
			for($i=1;$i<=5;$i++)
			{
				if($field['LOG_TEXT'.$i])
				{
					$count_fields++;
					$log_functions['LOG_TEXT'.$i] = $functions['LOG_TEXT'.$i];
					$columns['LOG_TEXT'.$i] = $field['LOG_TEXT'.$i.'_TITLE'];
					$link['add']['html']['LOG_TEXT'.$i] = $functions['LOG_TEXT'.$i]('','LOG_TEXT'.$i);
				}
			}
			if($field['LOG_TEXTAREA'])
			{
				if($count_fields<3)
					$textarea_extra = 'cols=60 rows=7';
				elseif($count_fields<5)
					$textarea_extra = 'cols=35 rows=4';
				else
					$textarea_extra = 'cols=25 rows=4';
					
				$log_functions['LOG_TEXTAREA'] = $functions['LOG_TEXTAREA'];
				$columns['LOG_TEXTAREA'] = $field['LOG_TEXTAREA_TITLE'];
				$link['add']['html']['LOG_TEXTAREA'] = $functions['LOG_TEXTAREA']('','LOG_TEXTAREA');
			}
			$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&include=$_REQUEST[include]&category_id=$_REQUEST[category_id]&modfunc=delete&LO_index=".$_REQUEST['LO_index'];
			$link['remove']['variables'] = array('id'=>'ID');
			$log_RET = DBGet(DBQuery("SELECT ID,LOG_CHECKBOX,LOG_SELECT,LOG_DATE,LOG_TEXT1,LOG_TEXT2,LOG_TEXT3,LOG_TEXT4,LOG_NUMERIC,LOG_TEXTAREA FROM STUDENT_LOG_ENTRIES WHERE STUDENT_ID='".UserStudentID()."' AND STUDENT_FIELD_ID='".$field['ID']."' ORDER BY ID ASC"),$log_functions);
			ListOutput($log_RET,$columns,$field['TITLE'].' '._('Entry'),$field['TITLE'].' '._('Entries'),$link,array(),array('search'=>false));
			echo '</TD></TR>';
		}
	}
	
	if(count($fields_RET['textarea']) || count($fields_RET['multiple']) || count($fields_RET['log']))
		echo '</TABLE>';
	
	echo '<BR>';
	
	if($radio_count)
	{
		echo '<TABLE cellpadding=5>';
		echo '<TR>';
		for($i=1;$i<=$radio_count;$i++)
		{
			$field = $fields_RET['radio'][$i];
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red class="font">*</font>';
			echo '<TD>'._makeCheckboxInput('CUSTOM_'.$field['ID'],'<b>'.$field['TITLE'].'</b>').'</TD>';
			if($i%5==0 && $i!=$radio_count)
				echo '</TR><TR>';
		}
		echo '</TD></TR>';
		echo '</TABLE>';
	}
}
elseif($_REQUEST['modfunc']!='delete')
{
	include_once($staticpath.'modules/Students/includes/functions.php');
	$fields_RET = DBGet(DBQuery("SELECT * FROM CUSTOM_FIELDS WHERE CATEGORY_ID='$_REQUEST[category_id]' ORDER BY SORT_ORDER"));
	if(count($fields_RET))
	{
		if(UserStudentID())
		{
			$custom_RET = DBGet(DBQuery("SELECT * FROM STUDENTS WHERE STUDENT_ID='".UserStudentID()."'"));
			$value = $custom_RET[1];
		}
		echo '<TABLE class=EntireBox width=100% cellpadding=6><TR bgcolor='.$bgcolor.'>';
		$count = 1;
		for($o=1;$o<=count($fields_RET);$o++)
		{
			$field = $fields_RET[$o];
			if($field['REQUIRED']=='Y')
				$field['TITLE'] .= ' <font color=red>*</font>';
			$colspan = 0;
			if(($count%3)==1)
			{
				if($fields_RET[$o+1]['TYPE']=='holder')
				{
					$count++;
					$o++;
					$colspan++;
				}
				if($fields_RET[$o+1]['TYPE']=='holder')
				{
					$count++;
					$o++;
					$colspan++;			
				}				
			}
			elseif($count%3==2 && $fields_RET[$o+1]['TYPE']=='holder')
			{
				$count++;
				$o++;
				$colspan++;			
			}
			if($colspan)
				$colspan = ' colspan='.($colspan+1);
			else
				$colspan = '';
			if($field['TYPE']=='log')
				$colspan .= ' bgcolor=white';
			
			echo '<TD'.$colspan.'>';
			switch($field['TYPE'])
			{
				case 'text':
					if($field['MAX_LENGTH'])
					{
						$options = ' maxlength='.$field['MAX_LENGTH'];
						if($field['MAX_LENGTH']<30)
							$options .= ' size='.$field['MAX_LENGTH'];
					}
					else
						$options = false;
		
					echo _makeTextInput('CUSTOM_'.$field['ID'],$field['TITLE'],$options);
				break;

				case 'numeric':
					echo _makeTextInput('CUSTOM_'.$field['ID'],$field['TITLE'],'','size=5 maxlength=10');
				break;

				case 'date':
					echo _makeDateInput('CUSTOM_'.$field['ID'],$field['TITLE'],'');
				break;
				
				case 'select':
					echo _makeSelectInput('CUSTOM_'.$field['ID'],$field['TITLE']);
				break;
				
				case 'textarea':
					echo _makeTextareaInput('CUSTOM_'.$field['ID'],$field['TITLE']);				
				break;
				
				case 'multiple':
					echo _makeMultipleInput('CUSTOM_'.$field['ID'],$field['TITLE']);
				break;
				
				case 'log':
					$link = array();
					$columns = array();
					$count_fields = 0;
					$log_options = $field['SELECT_OPTIONS'];
					$table = $field['ID'];
					$functions = array('LOG_CHECKBOX'=>'_makeLogCheckbox','LOG_SELECT'=>'_makeLogSelect','LOG_DATE'=>'_makeLogDate','LOG_TEXT1'=>'_makeLogText','LOG_TEXT2'=>'_makeLogText','LOG_TEXT3'=>'_makeLogText','LOG_TEXT4'=>'_makeLogText','LOG_NUMERIC'=>'_makeLogText','LOG_TEXTAREA'=>'_makeLogTextArea');
					if($field['LOG_CHECKBOX'])
					{
						$count_fields++;
						$log_functions['LOG_CHECKBOX'] = $functions['LOG_CHECKBOX'];
						$columns['LOG_CHECKBOX'] = $field['LOG_CHECKBOX_TITLE'];
						$link['add']['html']['LOG_CHECKBOX'] = $functions['LOG_CHECKBOX']('','LOG_CHECKBOX');
					}
					if($field['LOG_SELECT'])
					{
						$count_fields++;
						$log_functions['LOG_SELECT'] = $functions['LOG_SELECT'];
						$columns['LOG_SELECT'] = $field['LOG_SELECT_TITLE'];
						$link['add']['html']['LOG_SELECT'] = $functions['LOG_SELECT']('','LOG_SELECT');
					}
					if($field['LOG_DATE'])
					{
						$count_fields += 2;
						$log_functions['LOG_DATE'] = $functions['LOG_DATE'];
						$columns['LOG_DATE'] = $field['LOG_DATE_TITLE'];
						$link['add']['html']['LOG_DATE'] = $functions['LOG_DATE']('','LOG_DATE');
					}
					if($field['LOG_NUMERIC'])
					{
						$count_fields++;
						$log_functions['LOG_NUMERIC'] = $functions['LOG_NUMERIC'];
						$columns['LOG_NUMERIC'] = $field['LOG_NUMERIC_TITLE'];
						$link['add']['html']['LOG_NUMERIC'] = $functions['LOG_NUMERIC']('','LOG_NUMERIC');
					}
					for($i=1;$i<=5;$i++)
					{
						if($field['LOG_TEXT'.$i])
						{
							$count_fields++;
							$log_functions['LOG_TEXT'.$i] = $functions['LOG_TEXT'.$i];
							$columns['LOG_TEXT'.$i] = $field['LOG_TEXT'.$i.'_TITLE'];
							$link['add']['html']['LOG_TEXT'.$i] = $functions['LOG_TEXT'.$i]('','LOG_TEXT'.$i);
						}
					}
					if($field['LOG_TEXTAREA'])
					{
						if($count_fields<3)
							$textarea_extra = 'cols=60 rows=7';
						elseif($count_fields<5)
							$textarea_extra = 'cols=35 rows=4';
						else
							$textarea_extra = 'cols=25 rows=4';
							
						$log_functions['LOG_TEXTAREA'] = $functions['LOG_TEXTAREA'];
						$columns['LOG_TEXTAREA'] = $field['LOG_TEXTAREA_TITLE'];
						$link['add']['html']['LOG_TEXTAREA'] = $functions['LOG_TEXTAREA']('','LOG_TEXTAREA');
					}
					$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&include=$_REQUEST[include]&category_id=$_REQUEST[category_id]&modfunc=delete&LO_index=".$_REQUEST['LO_index'];
					$link['remove']['variables'] = array('id'=>'ID');
					$log_RET = DBGet(DBQuery("SELECT ID,LOG_CHECKBOX,LOG_SELECT,LOG_DATE,LOG_TEXT1,LOG_TEXT2,LOG_TEXT3,LOG_TEXT4,LOG_NUMERIC,LOG_TEXTAREA FROM STUDENT_LOG_ENTRIES WHERE STUDENT_ID='".UserStudentID()."' AND STUDENT_FIELD_ID='".$field['ID']."' ORDER BY ID ASC"),$log_functions);
					ListOutput($log_RET,$columns,$field['TITLE'].' '._('Entry'),$field['TITLE'].' '._('Entries'),$link,array(),array('search'=>false));
				break;

				case 'radio':
					echo _makeCheckboxInput('CUSTOM_'.$field['ID'],$field['TITLE']);				
				break;
				
				case 'holder':
				break;
			}
			echo '</TD>';
			if($count%3==0)
			{
				echo '</TR>';
				if($bgcolor==Preferences('COLOR'))
					$bgcolor = 'F8F8F9';
				else
					$bgcolor = Preferences('COLOR');
				echo '<TR bgcolor='.$bgcolor.'>';
			}
			$count++;
		}
		if($count%3==2)
			echo '<TD></TD><TD></TD>';
		elseif($count%3==0)
			echo '<TD></TD>';
		echo '</TR></TABLE>';
	}
}

?>